#!/usr/bin/env expect
############################################################################
# Purpose: Timing test for 1000 jobs.
############################################################################
# Copyright (C) 2012 SchedMD LLC
# Written by Danny Auble <da@schedmd.com>
#
# This file is part of Slurm, a resource management program.
# For details, see <https://slurm.schedmd.com/>.
# Please also read the included file: DISCLAIMER.
#
# Slurm is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# Slurm is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along
# with Slurm; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA.
############################################################################
source ./globals

set job_cnt     1000
set fanout      10
set test_script "$test_dir/test_script"

if {[get_config_param "FrontendName"] ne "MISSING" || $enable_memory_leak_debug != 0} {
	set job_cnt 2
}

proc cleanup {} {
	global scancel test_name

	run_command "$scancel -n $test_name"
}

make_bash_script $test_script "
remaining=\$1
while \[ \$remaining -gt 0 \]
do
	for i in \$(seq 1 $fanout); do
		$sbatch -J $test_name -o /dev/null --wrap $bin_true &
	done
	wait
	remaining=\$((\$remaining-$fanout))
done
"

#
# We use a bash script so we submit multiple jobs in parallel
# by backgrounding them in $fanout sized chunks
#
# NOTE: The throughput rate is highly dependent upon configuration
#
proc _submit_jobs {} {
	global test_script job_cnt squeue test_name

	run_command "$test_script $job_cnt"

	wait_for -fail -timeout 600 {!$jobs_found} {
		set output [run_command_output -fail -nolog "$squeue -o %j -n $test_name"]
		set jobs_found [regexp -all $test_name $output]
		if {$jobs_found} {
			log_debug "Still $jobs_found jobs remaining"
		}
	}
}

set time_took [string trim [time {_submit_jobs}] " per iteration microseconds"]
set jobs_per_sec [expr $job_cnt * 1000000 / $time_took]
log_debug "Ran $job_cnt jobs in $time_took microseconds or $jobs_per_sec jobs per second"
